package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.platform.feign.PlatformClient;
import com.wing.setting.model.entity.SysSetting;
import com.wing.setting.model.entity.SysSettingAdvert;
import com.wing.setting.model.request.SysSettingForm;
import com.wing.setting.model.response.SysSettingVO;
import com.wing.setting.service.SysSettingAdvertService;
import com.wing.setting.service.SysSettingService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Api(value = "Admin-刀具柜系统设置管理", tags = "#/cssabinet/sysSetting")
@RestController("sysSettingController")
@RequestMapping("cabinet/admin/sysSetting")
@RequiredArgsConstructor
public class SysSettingController {

    @Autowired
    private PlatformClient platformClient;

    private final SysSettingService sysSettingService;

    @Autowired
    private SysSettingAdvertService advertService;

    @ApiOperation(value = "查询所有刀具柜系统设置", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<SysSetting> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
    ) {

        Page<SysSetting> pg = new Page<>(page, limit);
        QueryWrapper<SysSetting> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }

        //搜索

        IPage<SysSetting> list = sysSettingService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/getOneByParams")
    public JsonResult getOneByParams(String enterpriseCode,SysSetting.Type type){
        QueryWrapper<SysSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_code",enterpriseCode);
        queryWrapper.eq("type",type.getValue());

        SysSetting sysSetting = sysSettingService.getOne(queryWrapper);
        return JsonResult.success(sysSetting);
    }


    @ApiOperation(value = "添加刀具柜系统设置", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody SysSettingForm sysSettingForm) {
        if (sysSettingService.add(sysSettingForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改刀具柜系统设置", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(SysSettingForm.Update.class) SysSettingForm sysSettingForm) {
        SysSetting sysSetting = sysSettingService.getById(sysSettingForm.getId());
        if (sysSetting == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(SysSetting.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(sysSettingForm, sysSetting, copyOptions);
        if (sysSettingService.updateById(sysSetting)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除刀具柜系统设置", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysSettingId", value = "刀具柜系统设置id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long sysSettingId) {
        if (sysSettingService.removeById(sysSettingId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }


    @ApiOperation(value = "查询所有刀具柜系统设置列表", notes = "搜索")
    @GetMapping("/getSettingListAdmin")
    public JsonResult<SysSettingVO> getSettingListAdmin(){
        List<SysSettingVO> voList = sysSettingService.getSettingListAdmin();
        return JsonResult.success(voList);
    }



    /**
     * 根据id关联查询
     */
    @GetMapping("/getSettingById")
    public JsonResult getSettingById(@RequestParam("id")Long id){
        SysSettingAdvert advert = advertService.getById(id);
        return JsonResult.success(advert);
    }
    /**
     * 修改详情
     */
    @PostMapping("/upDate")
    public JsonResult upDate(@RequestBody SysSettingVO sysSettingVO){

        if (!sysSettingService.upSetting(sysSettingVO)){
            return JsonResult.fail("修改失败");
        }
        return JsonResult.success("修改成功");
    }



    /*    @ApiOperation(value = "添加刀具柜系统设置", notes = "添加")
        @PostMapping("/addSetting")
        public JsonResult addSetting(@RequestBody SysSettingForm settingForm) {
            if (sysSettingService.addSetting(settingForm)) {
                return JsonResult.success("添加成功");
            }
            return JsonResult.fail("添加失败");
        }*/
    @ApiOperation(value = "添加刀具柜系统设置", notes = "添加")
    @PostMapping("/addSetting")
    public JsonResult addSetting(@RequestBody List<SysSettingForm> settingForm) {
        if (sysSettingService.addSetting(settingForm)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    /**
     * 根据id修改是否启用
     */
    @PutMapping("/upIsDelete")
    public JsonResult upIsDelete(@RequestParam("id") Long id,@RequestParam("isDeleted") Integer isDeleted){

        if (advertService.upIsDelete(id,isDeleted)){
            return JsonResult.success("修改成功");
        }
        return JsonResult.fail("修改失败");

    }

    /**
     * 查询刀具柜系统设置列表
     */
    @ApiOperation(value = "查询所有刀具柜系统设置列表", notes = "搜索")
    @GetMapping("/getSettingList")
    public JsonResult<SysSettingVO> getSettingList(){
        return JsonResult.success(platformClient.getSettingList());
    }

}
